<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px">
      <el-form-item label="设备IP" prop="ip">
        <ns-input
          v-model="queryParams.ip"
          class="!w-240px"
          clearable
          placeholder="请输入设备IP"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" class="!w-240px" clearable placeholder="请选择状态">
          <el-option :key="1" label="在线" :value="1" />
          <el-option :key="0" label="离线" :value="0" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="归属机构" prop="deptId">
        <el-cascader
          ref="deptCascader"
          v-model="queryParams.deptId"
          placeholder="请选择归属机构"
          :props="cascaderProps"
          :options="deptList"
          filterable
          clearable
        />
      </el-form-item> -->
      <!-- <el-form-item label="类型" prop="status">
        <el-select v-model="queryParams.status" class="!w-240px" clearable placeholder="请选择状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button @click="handleQuery"> 搜索 </el-button>
        <el-button @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <!-- <div>
      <el-button type="primary" @click="openForm('create')">新增</el-button>
      <el-button type="danger" plain @click="delHandle">删除</el-button>
    </div> -->
    <div class="device-wrap">
      <DeviceList
        :list="list"
        @get-list="handleQuery"
        @get-ids="getIds"
        @edit-item="
          ($event) => {
            openForm('update', $event.id)
          }
        "
      />
    </div>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.page"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  <DeviceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import DeviceForm from './DeviceForm.vue'
import * as DeviceApi from '@/api/device'
import DeviceList from './DeviceList/List.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { getSimpleDeptList } from '@/api/system/dept'
import { handleTree } from '@/utils/tree'

defineOptions({ name: 'DeviceList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const queryFormRef = ref() // 搜索的表单
const total = ref(10) // 列表的总页数
const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据
const ids = ref([]) // 选中的id
const queryParams = reactive({
  page: 1,
  pageSize: 10,
  ip: '',
  deptId: '',
  status: undefined
})
const deptList = ref([])
const cascaderProps = ref({
  checkStrictly: true,
  label: 'name',
  value: 'id',
  children: 'children',
  emitPath: false
})

async function getDeptList() {
  const data = await getSimpleDeptList()
  deptList.value = data
}
/** 获取并过滤选中的id */
const getIds = (e) => {
  if (ids.value.includes(e)) {
    ids.value.splice(ids.value.indexOf(e), 1)
  } else {
    ids.value.push(e)
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  console.log('id', id)
  formRef.value.open(type, id)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const res = await DeviceApi.devicePage(toRaw(queryParams))
    list.value = res.data
    total.value = res.count
  } finally {
    loading.value = false
  }
}

/** 新增设备 */
function addHandle() {
  console.log('新增设备')
}

/** 删除设备 */
async function delHandle() {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    const { code } = await DeviceApi.deviceDelete({ idList: ids.value })
    code == 200 && message.success(t('common.delSuccess'))
    code != 200 && (ids.value = [])
    // 刷新列表
    getList()
  } catch {}
}

onMounted(() => {
  getList()
  getDeptList()
})
</script>

<style lang="scss" scoped>
.device-wrap {
  margin-top: 20px;
}
</style>
